package com.example.admistrator.myvideoplayer;

import static com.example.admistrator.myvideoplayer.R.id.videoView;

import android.graphics.Rect;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.facebook.drawee.view.SimpleDraweeView;

public class MainActivity extends AppCompatActivity implements OnErrorListener {

  @BindView(R.id.iv_first_frame)
  SimpleDraweeView mIvFirstFrame;
  @BindView(videoView)
  VideoView mVideoView;
  @BindView(R.id.progressBar)
  ContentLoadingProgressBar mProgressBar;
  @BindView(R.id.iv_play)
  ImageView mIvPlay;
  @BindView(R.id.re_play)
  FrameLayout mRePlay;


  public String VIDEO_URL = "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/08/a6bf44c940d580cf809893f8131396c4.mp4";
//
//  private VideoView vv_video;
//  private MediaController mController;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_controller);
    ButterKnife.bind(this);

    mIvFirstFrame.setImageURI(
        "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/11/eba5790840501537803ad3644a828a7b.png");
    mIvPlay.setVisibility(View.VISIBLE);

    MediaController mediaController = new MediaController(this);
    mediaController.setEnabled(false);

    mVideoView.setMediaController(mediaController);
    mVideoView.setVisibility(View.GONE);







    //开始正式播放
    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
      @Override
      public void onPrepared(MediaPlayer mp) {
        int width = mp.getVideoWidth();
        int height = mp.getVideoHeight();
        Rect rect = new Rect();
        mIvFirstFrame.getGlobalVisibleRect(rect);
        mIvFirstFrame.setImageURI(
            "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/11/eba5790840501537803ad3644a828a7b.png");
        int nowWidth = rect.width();
        int nowHeight = nowWidth * height / width;

        resizeView((View) mIvFirstFrame.getParent(), nowWidth, nowHeight);
//                    resizeView(videoView, nowWidth, nowHeight);

        Log.e("MainActivity123", "onPrepared=开始播放");
        mProgressBar.setVisibility(View.GONE);

        int currentPosition = mVideoView.getCurrentPosition();
        int duration = mVideoView.getDuration();

        Log.e("MainActivity1", "onCreate=" + currentPosition + "=" + duration);

//        mVideoView.setVisibility(View.VISIBLE);
//        mVideoView.seekTo(0);
//        mVideoView.start();

      }
    });
//    mVideoView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//      @Override
//      public void onFocusChange(View v, boolean hasFocus) {
//        stopVideo();
//        mRePlay.setVisibility(View.GONE);
//      }
//    });

    //缓冲
    mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
      @Override
      public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
          case MediaPlayer.MEDIA_INFO_BUFFERING_START: //暂停播放等待缓冲更多数据
            mProgressBar.setVisibility(View.VISIBLE);
            break;
          case MediaPlayer.MEDIA_INFO_BUFFERING_END: //缓冲完后继续播放
            mProgressBar.setVisibility(View.GONE);

            Log.e("MainActivityddd", "onInfo=");
            break;
        }
        return false;
      }
    });

    //播放完毕
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
        mRePlay.setVisibility(View.VISIBLE);
        Log.e("MainActivity123", "onPrepared=开始完毕");

        //获取到的是最后一次看多进度条是的位置
        int currentPosition = mp.getCurrentPosition();

        //获取的是视频的总时间，单位ms
        int duration = mp.getDuration();

        //获取播放进度
        int bufferPercentage = mVideoView.getBufferPercentage();

        Log.e("MainActivity1  3", "onCreate=" + bufferPercentage + "=" + duration);

      }
    });

    //播放失败
    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
      @Override
      public boolean onError(MediaPlayer mp, int what, int extra) {
        Toast.makeText(getApplicationContext(), "此视频无法播放!", Toast.LENGTH_LONG).show();
        mProgressBar.setVisibility(View.GONE);
        mIvPlay.setVisibility(View.VISIBLE);
        return true;
      }
    });

    // 增加监听上一个和下一个的切换事件，默认这两个按钮是不显示的
//    mediaController.setPrevNextListeners(new OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        Toast.makeText(MainActivity.this, "下一个", Toast.LENGTH_SHORT).show();
//
//      }
//
//    }, new OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        Toast.makeText(MainActivity.this, "上一个", Toast.LENGTH_SHORT).show();
//      }
//    });

    mediaController.setPrevNextListeners(null, null);
//    try {
//      Class aClass = Class.forName("android.widget.MediaController");
//      Field mListenersSet = aClass
//          .getDeclaredField("mListenersSet");
//      mListenersSet.setAccessible(true);
//      mListenersSet.setBoolean(aClass, false);
//    } catch (NoSuchFieldException e) {
//      e.printStackTrace();
//    } catch (Exception e) {
//      e.printStackTrace();
//    }


  }
//
//  private void first() {
//    vv_video = (VideoView) findViewById(vv_video);
//    // 实例化MediaController
//    mController = new MediaController(this);
//    // 设置播放视频源的路径
//    vv_video.setVideoPath(
////        "http://192.168.3.215/Public/proofvideo/2017-08-04/ab8464cfa9c09e21571c37223e8a0830.mp4");
//        "http://192.168.3.215/Public/train/2017-08-03/1501743046.mp4");
//    // 为VideoView指定MediaController
//    vv_video.setMediaController(mController);
//    // 为MediaController指定控制的VideoView
//    mController.setMediaPlayer(vv_video);
//
//    vv_video.setOnErrorListener(this);
//
//    // 增加监听上一个和下一个的切换事件，默认这两个按钮是不显示的
//    mController.setPrevNextListeners(new OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        Toast.makeText(MainActivity.this, "下一个", Toast.LENGTH_SHORT).show();
//
//      }
//
//    }, new OnClickListener() {
//      @Override
//      public void onClick(View v) {
//        Toast.makeText(MainActivity.this, "上一个", Toast.LENGTH_SHORT).show();
//      }
//    });
//  }

  @Override
  public boolean onError(MediaPlayer mp, int what, int extra) {
    Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
    return false;
  }


  private static void resizeView(View view, int width, int height) {
    ViewGroup.LayoutParams layout = view.getLayoutParams();
    layout.width = width;
    layout.height = height;
    view.setLayoutParams(layout);
  }


  public void stopVideo() {
    mVideoView.pause();
    mVideoView.setVisibility(View.GONE);
    mProgressBar.setVisibility(View.GONE);
    mIvPlay.setVisibility(View.VISIBLE);
  }


  @OnClick(R.id.re_play)
  protected void onClickRePlay() {
    mVideoView.seekTo(0);
    mVideoView.start();
    mIvPlay.setVisibility(View.GONE);
    mRePlay.setVisibility(View.GONE);
  }

  @OnClick(R.id.iv_play)
  protected void onClickPlayButton() {

    Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();

    mIvPlay.setVisibility(View.GONE);
    mProgressBar.setVisibility(View.VISIBLE);
    mVideoView.setVisibility(View.VISIBLE);
    mVideoView.setVideoPath(
        "http://bmob-cdn-7694.b0.upaiyun.com/2016/12/08/a6bf44c940d580cf809893f8131396c4.mp4");
    mVideoView.requestFocus();
    mVideoView.seekTo(0);
    mVideoView.start();
  }
}
